// 修复 Room 无法生成实现类的问题，确保只保留一个构造函数
// 使用普通 class 并提供一个空构造函数供 Room 使用

package cn.nexttec.noteplayer.models

import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import cn.nexttec.noteplayer.common.formatName
import cn.nexttec.noteplayer.common.md5
import java.io.File
import java.io.Serializable
import java.util.Date

@Entity(tableName = "VideoFile")
class VideoFileInfo : Serializable {
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "_id")
    var _id: Long = 0L

    @ColumnInfo(name = "Name")
    var name: String = ""

    @ColumnInfo(name = "Path")
    var path: String = ""

    @ColumnInfo(name = "Parent")
    var parent: String = ""

    @ColumnInfo(name = "ThumbImage")
    var thumbImage: String = ""

    @ColumnInfo(name = "NameWithoutExtension")
    var nameWithoutExension: String = ""


    @ColumnInfo(name = "Depth")
    var depth: Int = 0

    @ColumnInfo(name = "FileSize")
    var fileSize: Long = 0L

    @ColumnInfo(name = "CreatedAt")
    var createdAt: Long = 0L

    @ColumnInfo(name = "ModifiedAt")
    var modifiedAt: Long = 0L

    @ColumnInfo(name = "IsVideoFile")
    var isVideoFile: Boolean = false

    @ColumnInfo(name = "IsDirectory")
    var isDirectory: Boolean = false

    @ColumnInfo(name = "VideoMd5")
    var videoMd5: String = ""

    @ColumnInfo(name = "FormatedName")
    var formatedName: String = ""

    @ColumnInfo(name = "Private")
    var isPrivate: Boolean = false


    @ColumnInfo(name = "NoteName")
    var noteName: String = ""

    @ColumnInfo(name = "NoteFilePath")
    var noteFilePath: String = ""

    @ColumnInfo(name = "NoteCreatedAt")
    var noteCreatedAt: Date = Date()

    @ColumnInfo(name = "NoteModifiedAt")
    var noteModifiedAt: Date = Date()

    @ColumnInfo(name = "Tip")
    var tip: String = ""

    constructor()

    @Ignore
    constructor(
        _id: Long = 0L,
        name: String = "",
        nameWithoutExension: String = "",
        path: String = "",
        parent: String = "",
        thumbImage: String = "",
        depth: Int = 0,
        fileSize: Long = 0L,
        createdAt: Long = 0L,
        modifiedAt: Long = 0L,
        isVideoFile: Boolean = false,
        isDirectory: Boolean = false,
        tip: String = ""
    ) {
        this._id = _id
        this.name = name
        this.nameWithoutExension = nameWithoutExension
        this.formatedName = formatName(nameWithoutExension)
        this.path = path
        this.parent = parent
        this.thumbImage = thumbImage
        this.depth = depth
        this.fileSize = fileSize
        this.createdAt = createdAt
        this.modifiedAt = modifiedAt
        this.isVideoFile = isVideoFile
        this.isDirectory = isDirectory
        this.videoMd5 = path.md5()
        this.tip = tip
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is VideoFileInfo) return false

        return path == other.path  // 或者根据你认为唯一标识的字段，比如 videoMd5、_id
    }

    override fun hashCode(): Int {
        return path.hashCode()  // 和 equals 中保持一致
    }

    companion object {
        fun fromDirectory(file: File, depth: Int, tip: String): VideoFileInfo {
            return VideoFileInfo(
                name = file.name,
                nameWithoutExension = file.name,
                path = file.absolutePath,
                parent = file.parent ?: "",
                depth = depth,
                modifiedAt = file.lastModified(),
                isDirectory = true,
                tip = tip
            )
        }

        fun fromVideoFile(file: File, depth: Int): VideoFileInfo {
            return VideoFileInfo(
                name = file.name,
                nameWithoutExension = file.nameWithoutExtension,
                path = file.absolutePath,
                parent = file.parent ?: "",
                depth = depth + 1,
                fileSize = file.length(),
                modifiedAt = file.lastModified(),
                isVideoFile = true,
            )
        }

        fun List<VideoFileInfo>.sort(method: SortMethod): List<VideoFileInfo> {
            return when (method) {
                SortMethod.FILENAME_ASC -> {
                    sortedBy { it.formatedName }
                }

                SortMethod.FILENAME_DESC -> {
                    sortedBy { it.formatedName.reversed() }
                }

                SortMethod.FILE_TIME_ASC -> {
                    sortedBy { it.createdAt }
                }

                SortMethod.FILE_TIME_DESC -> {
                    sortedBy { it.createdAt }.reversed()
                }

                SortMethod.FILE_SIZE_ASC -> {
                    sortedBy { it.fileSize }
                }

                SortMethod.FILE_SIZE_DESC -> {
                    sortedBy { it.fileSize }.reversed()
                }
            }
        }

    }


}

enum class SortMethod(val value: Int) {
    FILENAME_ASC(0),
    FILENAME_DESC(1),
    FILE_TIME_ASC(2),
    FILE_TIME_DESC(3),
    FILE_SIZE_ASC(4),
    FILE_SIZE_DESC(5);

    fun getReversedOrder(): SortMethod {
        return when (this) {
            FILENAME_ASC -> FILENAME_DESC
            FILENAME_DESC -> FILENAME_ASC
            FILE_SIZE_ASC -> FILE_SIZE_DESC
            FILE_SIZE_DESC -> FILE_SIZE_ASC
            FILE_TIME_ASC -> FILE_TIME_DESC
            FILE_TIME_DESC -> FILE_TIME_ASC
        }
    }

    companion object {
        private val map = values().associateBy(SortMethod::value)

        fun fromInt(value: Int): SortMethod? = map[value]
    }
}
